package apps.web.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import webframe.common.helper.ControllerHelper;
import webframe.common.helper.FormatTransformHelper;
import apps.web.controller.base.MyBaseCommandController;
import apps.web.model.Apply;
import apps.web.model.ApplyCensor;
import apps.web.model.User;

public class ApplyCensorSubmitController extends MyBaseCommandController {

	@Override
	protected ModelAndView handleInner(HttpServletRequest request,
			HttpServletResponse response, Object command)
			throws RuntimeException {
		ApplyCensor applyCensor = (ApplyCensor) command;

		applyCensor.setUpdateDate(new Date());
		applyCensor.setUsername(ControllerHelper.getUserBySession()
				.getUsername());

		applyCensor.setUserId(((User) ControllerHelper.getUserBySession())
				.getId());

		Apply apply = getServiceContainer().getApplyBizService().findApplyById(
				applyCensor.getApplyId());
		apply.setReason((String) applyCensor.getExtraObject("reason"));
		if (applyCensor.getExtraObject("inspectorId").getClass().isArray()) {
			apply.setInspectorId(FormatTransformHelper
					.getStringFromArray((String[]) applyCensor
							.getExtraObject("inspectorId")));
		} else {
			apply.setInspectorId((String) applyCensor
					.getExtraObject("inspectorId"));
		}
		if (applyCensor.getCensorKind().equals("305")) {
			apply.setApplyFlag(4L);
		} else if (applyCensor.getCensorKind().equals("307")) {
			if (applyCensor.getCensorResult().intValue() == 3) {
				apply.setApplyFlag(11L);
			} else if (applyCensor.getCensorResult().intValue() == 5) {
				apply.setApplyFlag(9L);
			}
		}
		apply.setApplyCode((String) applyCensor.getExtraObject("applyCode"));

		 getServiceContainer().getApplyBizService().updateApply(apply);
		getServiceContainer().getApplyBizService().saveApplyCensor(applyCensor);

		return new ModelAndView("ApplySearch.do");
	}

}